package com.project.xsf.service.impl;

import com.project.xsf.entity.MDesignProcedureDetails;
import com.project.xsf.entity.MDesignProcedureModule;
import com.project.xsf.mapper.MDesignProcedureDetailsMapper;
import com.project.xsf.mapper.MDesignProcedureModuleMapper;
import com.project.xsf.service.MDesignProcedureDetailsService;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.mapper.DModuleDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class MDesignProcedureDetailsImpl implements MDesignProcedureDetailsService {
    @Autowired
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;
    @Autowired
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;
    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;
    @Override
    public int insertSelective(MDesignProcedureDetails record) {
        return mDesignProcedureDetailsMapper.insertSelective(record);
    }

    @Override
    public List<MDesignProcedureDetails> queryMDesignProcedureDetailsByPId(Integer pId) {
        return mDesignProcedureDetailsMapper.queryMDesignProcedureDetailsByPId(pId);
    }

    @Override
    public MDesignProcedureDetails selectByPrimaryKey(Integer id) {
        return mDesignProcedureDetailsMapper.selectByPrimaryKey(id);
    }

    @Override
    public MDesignProcedureDetails queryAndUpdateMDesignProcedureDetailsById(Integer mDesignProcedureDetailId,String mDesignProcedurePId,String productId) {
        //重新设计时将删除该工序对应的物料信息，并更新物料的可用数量
//        mDesignProcedureModuleMapper.deleteByPId(mDesignProcedureDetailId);

        List<DModuleDetails> dModuleDetails = dModuleDetailsMapper.queryDModuleDetailsByProductId(productId);

        for(int i=0;i<dModuleDetails.size();i++){
            Integer desidualAmount;
            DModuleDetails dModuleDetail = dModuleDetails.get(i);
            List<MDesignProcedureModule> mDesignProcedureModules = mDesignProcedureModuleMapper.queryMDesignProcedureModuleByProductId(dModuleDetail.getProductId(), mDesignProcedureDetailId);

            desidualAmount = mDesignProcedureModules.size()==0?dModuleDetail.getAmount():0;
            for(int j=0;j<mDesignProcedureModules.size();j++){
                MDesignProcedureModule mDesignProcedureModule = mDesignProcedureModules.get(j);
//                System.out.println(mDesignProcedureModule);
                desidualAmount=dModuleDetail.getAmount()-mDesignProcedureModule.getAmount();
//                System.out.println(desidualAmount);
            }
            dModuleDetail.setDesidualAmount(desidualAmount);
//            System.out.println(dModuleDetail);

            dModuleDetailsMapper.updateByPrimaryKeySelective(dModuleDetail);
        }


        return mDesignProcedureDetailsMapper.selectByPrimaryKey(mDesignProcedureDetailId);
    }

    @Override
    public int updateByPrimaryKeySelective(MDesignProcedureDetails record) {
        return mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<MDesignProcedureDetails> queryMDesignProcedureDetailsByProductId(String productId) {
        return mDesignProcedureDetailsMapper.queryMDesignProcedureDetailsByProductId(productId);
    }


}
